package edu.ynmd.com.service.impl;

import edu.ynmd.com.dao.AreaDao;
import edu.ynmd.com.dao.CityDao;
import edu.ynmd.com.dao.ProvinceDao;
import edu.ynmd.com.dao.StuinfoDao;
import edu.ynmd.com.model.Area;
import edu.ynmd.com.model.City;
import edu.ynmd.com.model.Province;
import edu.ynmd.com.model.Stuinfo;
import edu.ynmd.com.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    public StuinfoDao stuinfoDao;

    @Autowired
    public ProvinceDao provinceDao;

    @Autowired
    public CityDao cityDao;

    @Autowired
    public AreaDao areaDao;

    @Override
    public boolean addStuinfo(Stuinfo stuinfo) {
        try {
            stuinfoDao.save(stuinfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteStuinfo(String stuinfoid) {
        try {
            stuinfoDao.deleteById(stuinfoid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean updateStuinfo(Stuinfo stuinfo) {
        try {
            stuinfoDao.save(stuinfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public Stuinfo getSingleStuinfo(String stuinfoid) {
        try {
            Optional<Stuinfo> optionalStuinfo=stuinfoDao.findById(stuinfoid);
            return optionalStuinfo.get();
        }catch (Exception e){
            return null;
        }

    }

    @Override
    public List<Stuinfo> getStuinfoList() {
        return stuinfoDao.findAll();
    }

    @Override
    public String getProvinceNameByCode(String sfbm) {
        List<Province> p1=provinceDao.getProvinceByCode(sfbm);
        if (p1.size()>0){
            return p1.get(0).getName();

        }else {
            return "";
        }
    }

    @Override
    public String getCityNameByCode(String citycode) {
        List<City> c1=cityDao.getCitiesByCode(citycode);
        if (c1.size()>0){
            return c1.get(0).getName();
        }
        return "";
    }

    @Override
    public String getAreaNameByCode(String areaCode) {
        List<Area> a1=areaDao.getAreasByCode(areaCode);
        if (a1.size()>0){
            return a1.get(0).getName();
        }
        return "";
    }

    @Override
    public String getSexNameByStr(String sexstr) {
        int si=Integer.parseInt(sexstr);
        if (si%2==0){
            return "女";
        }else {
            return "男";
        }

    }
}
